package it.unisalento.appsalento.activity;

import it.unisalento.appsalento.activity.fragment.CalendarioFragment;
import it.unisalento.appsalento.activity.fragment.HomeFragment;
import it.unisalento.appsalento.activity.fragment.LibrettoFragment;
import it.unisalento.appsalento.activity.fragment.RisultatiFragment;
import it.unisalento.appsalento.bo.mock.MockBo;
import it.unisalento.appsalento.model.Appello;
import it.unisalento.appsalento.model.Esame;
import it.unisalento.appsalento.model.Notizia;
import it.unisalento.appsalento.model.Utente;
import it.unisalento.appsalento.constants.Constants;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * <b>Description:</b>Classe contenente i dati da mantenere in sessione
 * 
 * @author AGM
 */
public class App extends Application {
	public static MockBo mockBo;
	public static Utente utente;
	public static ArrayList<Notizia> news;
	public static List<Appello> appelli;
	public static ArrayList<Appello> appelliForDay;
	public static ArrayList<Esame> listaEsami;
	public static HomeFragment homeFragment;
	public static LibrettoFragment librettoFragment;
	public static CalendarioFragment calendarioFragment;
	public static Fragment dettaglioEsameFragment;
	public static RisultatiFragment risultatiFragment;
	public static Esame esameCorrente;
	public static Appello dettaglioAppello;
	public static String registrationId;

	private static Context context;
	private static Activity activity;
	
	public static Dialog mRisultatoDialog;
	public static Dialog mAppelloDialog;
	
	public static int calendarActualYear;
	public static int calendarActualMonth;

	public static final String PROPERTY_REG_ID = "registration_id";
	private static final String PROPERTY_APP_VERSION = "appVersion";

	@Override
	public void onCreate() {
		super.onCreate();
		App.context = getApplicationContext();
		news = new ArrayList<Notizia>();
		appelli = new ArrayList<Appello>();
		listaEsami = new ArrayList<Esame>();
		appelliForDay = new ArrayList<Appello>();
		calendarActualYear = Calendar.getInstance().get(Calendar.YEAR);
		calendarActualMonth = Calendar.getInstance().get(Calendar.MONTH);
	}

	public synchronized static Context getAppContext() {
		return App.context;
	}

	public static void setCurrentActivity(Activity currentActivity) {
		activity = currentActivity;
	}

	public static Activity currentActivity() {
		return activity;
	}

	public static void displayNotifyMessage(Context context, String message) {
		Intent intent = new Intent(Constants.DISPLAY_MESSAGE_ACTION);
		intent.putExtra(Constants.EXTRA_MESSAGE, message);
		context.sendBroadcast(intent);
	}

	/**
	 * @return Il codice di versione dell'applicazione
	 */
	public static int getAppVersion(Context context) {
		try {
			PackageInfo packageInfo = context.getPackageManager()
					.getPackageInfo(context.getPackageName(), 0);
			return packageInfo.versionCode;
		} catch (NameNotFoundException e) {
			// should never happen
			throw new RuntimeException("Could not get package name: " + e);
		}
	}

	/**
	 * Ritorna il registrationId a GCM corrente
	 * 
	 * @return registration ID
	 */
	public static String getRegistrationId(Context context) {
		final SharedPreferences prefs = getGCMPreferences(context);
		String registrationId = prefs.getString(PROPERTY_REG_ID, "");
		if (registrationId.isEmpty()) {
			return "";
		}
		// Se l'applicazione e' stata aggiornata, cancelliamo 
		// il registrationId per aggiornarlo
		int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
				Integer.MIN_VALUE);
		int currentVersion = App.getAppVersion(context);
		if (registeredVersion != currentVersion) {
			return "";
		}
		return registrationId;
	}
	
	/** 
	 * Salva il registrationId a GCM nelle shared preferences 
	 * 
	 * @param context Il context dell'applicazione 
	 * @param regId L'ID di registrazione
	 */ 
	public static void storeRegistrationId(Context context, String regId) {
	    final SharedPreferences prefs = getGCMPreferences(context);
	    int appVersion = getAppVersion(context);
	    
	    SharedPreferences.Editor editor = prefs.edit();
	    editor.putString(PROPERTY_REG_ID, regId);
	    editor.putInt(PROPERTY_APP_VERSION, appVersion);
	    editor.commit();
	}
	
	/** 
	 * Cancella il registrationId a GCM dalle shared preferences 
	 * 
	 * @param context Il context dell'applicazione 
	 */ 
	public static void deleteRegistrationId(Context context) {
	    final SharedPreferences prefs = getGCMPreferences(context);
	    
	    SharedPreferences.Editor editor = prefs.edit();
	    editor.remove(PROPERTY_REG_ID);
	    editor.commit();
	}

	/**
	 * @return Le shared preferences dell'applicazione
	 */
	private static SharedPreferences getGCMPreferences(Context context) {
		// This sample app persists the registration ID in shared preferences,
		// but
		// how you store the regID in your app is up to you.
		return currentActivity().getSharedPreferences(
				MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
	}
}
